// index.js
// 获取应用实例
const app = getApp()
var self;
var timer;

Page({
    data: {
        token: '',
        is_source: false,
        source_time: 5000,
        led_master: false,
        led_slave: false,
        th: 20,
        rh: 20,
        lux: 20,
    },

    onLoad() {
        self = this;
        self.get_token();
    },
    get_timer: function(e){
        this.setData({
            source_time: e.detail.value,
        });
    },
    // 事件处理函数
    bind_switch(e) {
        var sw_id = e.currentTarget.id;
        var sw_value = e.detail.value;
        if(sw_id=="led_master" || sw_id=="led_slave"){
            self.ctl_led(sw_id, sw_value);
        }
        else if(sw_id == "is_source"){
            if(sw_value){
                timer = setInterval(self.query_data, self.data.source_time);
            }else{
                clearInterval(timer);
            }
        }
    },

    //获取认证token
    get_token: function(){
        wx.request({
            url: 'https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens', //仅为示例，并非真实的接口地址
            method: 'POST',
            data: {
                "auth": {
                    "identity": {
                        "methods": [
                            "password"
                        ],
                        "password": {
                            "user": {
                                "domain": {
                                    "name": "wj78080458"
                                },
                                "name": "weijie",
                                "password": "weijie543092"
                            }
                        }
                    },
                    "scope": {}
                }
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                //将结果转换为字符串
                var json = JSON.stringify(res.header);
                //替换内容
                json = json.replace("X-Subject-Token", "xtoken");
                //重新转换为json格式
                json = JSON.parse(json);
                //提取数据
                var xtoken = json.xtoken;
                //console.log(json.xtoken);
                self.setData({ token: xtoken });
                console.log(self.data.token);
            }
        });

    },
    //查询属性
    query_data: function(){
        wx.request({
            url: 'https://a160a780fe.iotda.cn-north-4.myhuaweicloud.com:443/v5/iot/0c7039e2db00f2a42fe4c016707a42b1/devices/633a9fbb3ec34a6d03c667c7_2022100319/properties?service_id=SENSOR',
            method: 'GET',
            data: {

            },
            header: {
                "X-Auth-Token": self.data.token,
				"Content-Type": "application/json"
            },
            success: function (res) {
                self.setData({
                    lux: res.data.response.services[0].properties.LUX,
                    th: res.data.response.services[0].properties.TH,
                    rh: res.data.response.services[0].properties.RH,
                });
                console.log(res.data.response.services[0].properties.LUX);
            }
        })
    },
        //查询属性
    ctl_led: function (led, state) {
        var command_name = "LED1_CTL";
        var led_state = "ON";
        if(led == "led_master"){
            command_name = "LED1_CTL";
            if(state){
                led_state = "ON";
            }else{
                led_state = "OFF";
            }
        } else if (led == "led_slave") {
            command_name = "LED2_CTL";
            if (state) {
                led_state = "ON";
            } else {
                led_state = "OFF";
            }
        }
        wx.request({
            url: 'https://a160a780fe.iotda.cn-north-4.myhuaweicloud.com:443/v5/iot/0c7039e2db00f2a42fe4c016707a42b1/devices/633a9fbb3ec34a6d03c667c7_2022100319/commands',
            method: 'POST',
            data: {
                "service_id": "LED",
                "command_name": command_name,
                "paras": {
                    "STATE_CTL": led_state,
                }
            },
            header: {
                "X-Auth-Token": self.data.token,
                "Content-Type": "application/json"
            },
            success: function (res) {
                console.log(res);
            }
        })
    }
})